package com.yaps.petstore.managedbean;

import com.yaps.petstore.entity.catalog.Category;
import com.yaps.petstore.entity.catalog.Item;
import com.yaps.petstore.entity.catalog.Product;
import com.yaps.petstore.service.CatalogService;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @author Antonio Goncalves
 */
@Component("catalog")
@Scope("session")
public class CatalogController extends Controller{

    // ======================================
    // =             Attributs              = 
    // ======================================
    @Autowired
    private CatalogService catalogService;

    private final String cname = this.getClass().getName();

    private String keyword;
    private Category category;
    private Product product;
    private Item item;
    private List<Product> products;
    private List<Item> items;

    // ======================================
    // =             Constantes             =
    // ======================================

    // ======================================
    // =            Constructeurs           =
    // ======================================

    // ======================================
    // =          Methodes publiques        = 
    // ======================================

    public String doFindProducts() {
        final String mname = "doFindProducts";
        logger.entering(cname, mname);

        String navigateTo = null;

        try {
            category = catalogService.findCategory(getParamId("categoryId"));
            products = category.getProducts();
            navigateTo = "/showproducts.jsf?faces-redirect=true";
        } catch (Exception e) {
            addMessage(cname, mname, e);
        }

        logger.exiting(cname, mname, navigateTo);
        return navigateTo;
    }

    public String doFindItems() {
        final String mname = "doFindItems";
        logger.entering(cname, mname);

        String navigateTo = null;

        try {
            product = catalogService.findProduct(getParamId("productId"));
            items = product.getItems();
            navigateTo = "items.displayed";
        } catch (Exception e) {
            addMessage(cname, mname, e);
        }

        logger.exiting(cname, mname, navigateTo);
        return "/showitems.jsf?faces-redirect=true";
    }

    public String doFindItem() {
        final String mname = "doFindItem";
        logger.entering(cname, mname);

        String navigateTo = null;

        try {
            item = catalogService.findItem(getParamId("itemId"));
            navigateTo = "item.displayed";
        } catch (Exception e) {
            addMessage(cname, mname, e);
        }

        logger.exiting(cname, mname, navigateTo);
        return "/showitem.jsf?faces-redirect=true";
    }

    public String doSearch() {
        final String mname = "doSearch";
        logger.entering(cname, mname);

        String navigateTo = null;

        try {
            items = catalogService.searchItems(keyword);
            navigateTo = "/searchresult.jsf?faces-redirect=true";
        } catch (Exception e) {
            addMessage(cname, mname, e);
        }

        logger.exiting(cname, mname, navigateTo);
        return navigateTo;
    }

    // ======================================
    // =          Methodes Protégées        = 
    // ======================================

    // ======================================
    // =             Accesseurs             = 
    // ======================================

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }


    public List<Product> getProducts() {
        return products;
    }

    public List<Item> getItems() {
        return items;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    // ======================================
    // =           Methodes Privées         =
    // ======================================

    // ======================================
    // =   Methodes hash, equals, toString  =
    // ======================================
}
